<?php

class Module {

	var $db;

	function __construct (){

	}

	public function set_db($db){
		$this->db = $db;
	}

	function get($table, $where){
		$sql = "SELECT * FROM $table WHERE $where";
		$line = $this->db->queryUniqueObject($sql);

		if($line){
			foreach($line as $k => $v){
				$result[$k] = $v;
			}
		}

		return $result;
	}

	function empty_field($table, $field, $where = 1){
		if(is_array($field)){
			foreach($field as $k => $v){
				$tmp .= "$v = '', ";
			}

			$tmp = substr($tmp, 0, -2);
			$field = $tmp;
		}else{
			$field = "$field = ''";
		}
		
		$sql = "UPDATE $table SET $field WHERE $where";
		return $this->db->execute($sql);
	}

	function get_all($table, $where = 1){

		$result = array();

		$sql = "SELECT * FROM $table WHERE $where";
		$rss = $this->db->query($sql);

		while($line = $this->db->fetchNextObject($rss)){

			foreach($line as $k => $v){
				$tmp[$k] = $v;
			}

			array_push($result, $tmp);
		}

		return $result;
	}

	function to_string(){

	}

	function fetch($line) {
		foreach($line as $k => $v){
			$result[$k] = $v;
		}

		return $result;
	}

	function sql_where($where)
	{

		if( is_array($where) )
		{
			
			foreach($where as $wkey => $wtemp){
				
				$cond_where .= " and ";
				switch ($wtemp['1'])
				{
					case "like":
							$cond_where .= " {$wkey} like '%".$wtemp[0]."%' ";
					break;
					case "between":
							$cond_where .= " {$wkey} between ".$wtemp[0]." ";
					break;
					case "!=":
							$cond_where .= " {$wkey} != '".$wtemp[0]."' ";
					break;
					case "in":
							$cond_where .= " {$wkey} in (".$wtemp[0].") ";
					break;

					default:
							$c = (is_array($wtemp))?$wtemp[0]:$wtemp;
							$cond_where .= " {$wkey} = '". $c ."' ";
					break;
				}
			}
		}
		return $cond_where;
	}


	function sql_order($order){

		if( is_array($order) && count($order)>0 )
		{
			$cond_order = "order by ";
			foreach($order as $okey => $ovalue){
				
				if(ereg('\(|\)', $ovalue, $match))
				{
					$cond_order .= " FIELD ".$ovalue.", ";
				}else{
					$cond_order .= "{$okey} ".$ovalue.", ";
				}
				
			}
			$cond_order = substr($cond_order, 0, -2);
		}
		return $cond_order;
	}

	function sql_limit($limit){

		if( $limit['limit'] != "")
		{
			$cond_limit = " limit ".$limit['limit'];
		}
		return $cond_limit;
	}
	
}

?>